package com.qidi.web;

import com.qidi.pojo.*;
import com.qidi.service.BookService;
import com.qidi.service.OrderService;
import com.qidi.service.impl.BookServiceimpl;
import com.qidi.service.impl.OrderServiceimpl;
import com.qidi.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author zhouXinBin
 * @create 2020-09-04-14:16
 */
public class OrderServlet extends BaseServlet {
    OrderService orderService=new OrderServiceimpl();
    protected void createOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BookService bookService=new BookServiceimpl();
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        User user = (User) request.getSession().getAttribute("user");
        if (cart!=null && user!=null){
            String orderid = orderService.createOrder(cart, user.getId());
            Map<Integer, CartItem> items = cart.getItems();
            for (CartItem item : items.values()) {
                Book book = bookService.queryBookById(item.getId());
                book.setSales(book.getSales()+item.getCount());
                book.setStock(book.getStock()-item.getCount());
                bookService.updateBook(book);
            }
            request.getSession().setAttribute("orderid",orderid);
            response.sendRedirect(request.getContextPath()+"/pages/cart/checkout.jsp");
        }
    }

    protected void queryAllOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Order> orders = orderService.queryAllOrders();
        request.setAttribute("Allorders",orders);
        request.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(request,response);
    }

    protected void queryOrderItems(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<OrderItem> orderitems = orderService.queryOrderItems(request.getParameter("orderid"));
        request.setAttribute("orderitems",orderitems);
        request.getRequestDispatcher("/pages/order/orderdetails.jsp").forward(request,response);
    }

    protected void queryMyOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        List<Order> myOrder = orderService.queryMyOrder(user.getId());
        request.setAttribute("myorder",myOrder);
        request.getRequestDispatcher("/pages/order/order.jsp").forward(request,response);
    }

    protected void changeOrderStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int status = WebUtils.parseInt(request.getParameter("status"),-1);
        String orderid = request.getParameter("orderid");
        if (status==1){
            status=2;
        }
        if (status==0){
            status=1;
        }

        orderService.changeOrderStatus(status,orderid);
        response.sendRedirect(request.getHeader("Referer"));
    }



}
